<?php
/**
 * Created by PhpStorm.
 * User: qilin
 * Date: 2018/1/29
 * Time: 11:20
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Service\AdminService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    private $adminService;
    public function __construct(AdminService $adminService)
    {
        $this->adminService = $adminService;
    }
    //登录
    public function adminLogin(Request $request){
        $adminData = $request->only('admin_login_name','admin_password');
        $validator = Validator::make($adminData,[
            'admin_login_name'=>'required',
            'admin_password'=>'required'
        ],[
            'admin_login_name.required'=>'请输入登录账号',
            'admin_password.required'=>'请输入登录密码'
        ]);
        if($validator->fails()){
            $error = current((array)json_decode(json_encode($validator->errors())));
            return view("login")->with('error',$error[0]);
        }else{
            $result = $this->adminService->selectAdmin($adminData);
            if(!is_null($result)){
                $adminId = $result->toArray()['admin_id'];
                $request->session()->put('isLogin', $adminId);
                return redirect('index');
            }else{
                $error = '账号或者密码错误';
                return view("login")->with('error',$error);
            }
        }
    }
    //主页
    public function index(Request $request){
        $adminId = $request->session()->get("isLogin");
        $adminData =$this->adminService->getAdmin($adminId)->toArray();
        return view('index')->with('data',$adminData);
    }
    //注销
    public function adminLogout(Request $request){
        $request->session()->flush();
        return redirect('/');
    }
    //获取管理员列表
    public function getAdminList(){
        $data = $this->adminService->getAdminList();
        return view('admin_list')->with('data',$data);
    }
    //删除管理员
    public function deleteAdmin(Request $request){
        $id = $request->get('id');
        $this->adminService->deleteAdmin($id);
        return redirect('getAdminList');
    }
    //修改管理员
    public function updateAdmin(Request $request){
        $id = $request->get('id');
        $data = $this->adminService->getAdmin($id);
        return view('admin_update')->with('data',$data);
    }
    //执行修改管理员
    public function doUpdateAdmin(Request $request){
        $data = $request->all();
        $this->adminService->updateAdmin($data);
        return redirect('getAdminList');
    }
    //添加管理员
    public function addAdmin(){
        return view('admin_add');
    }
    //执行添加管理员
    public function doAddAdmin(Request $request){
        $data = $request->all();
        $this->adminService->addAdmin($data);
        return redirect('getAdminList');
    }
    public function getSystemInfo(){
        $data = $this->adminService->getSystem();
        return view('system_info')->with('data',$data);
    }
}